package com.jia.lidao.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.jia.lidao.R;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.ForgetPswRequest;
import com.jia.lidao.network.http.request.impl.ForgetPswSmsRequest;
import com.jia.lidao.network.http.request.impl.RegisterRequest;
import com.jia.lidao.network.http.request.impl.RegisterSmsRequest;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.widget.CommonTitleBar;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by jia on 17/12/17.
 */

public class RegisterActivity extends BaseActivity
		implements View.OnClickListener {

	private EditText etPhone;
	private EditText etSms;
	private TextView tvSendSms;
	private EditText etPsd;
	private EditText etPsdAgain;
	private TextView tvTip;
	private TextView tvRegister;
	private TextView tvTitle;
	private ImageView topbarLeft;

	private CommonTitleBar mCommonTitleBar;

	private boolean isForgetPSWD;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		isForgetPSWD = getIntent().getBooleanExtra("isForgetPSWD", false);
		initView();
	}

	private void initView() {
		etPhone = (EditText) findViewById(R.id.et_phone);
		etSms = (EditText) findViewById(R.id.et_sms);
		tvSendSms = (TextView) findViewById(R.id.tv_send_sms);
		etPsd = (EditText) findViewById(R.id.et_psd);
		etPsdAgain = (EditText) findViewById(R.id.et_psd_again);
		tvTip = (TextView) findViewById(R.id.tv_tip);
		tvRegister = (TextView) findViewById(R.id.tv_register);
		tvTitle = (TextView) findViewById(R.id.tv_title);

		mCommonTitleBar = (CommonTitleBar) findViewById(R.id.common_title_bar);

		mCommonTitleBar.mBackIv.setImageResource(R.mipmap.registration_back);
		findViewById(R.id.tv_register).setOnClickListener(this);
		findViewById(R.id.tv_send_sms).setOnClickListener(this);
		if (isForgetPSWD) {
			tvTitle.setText("忘记密码");
			tvTip.setVisibility(View.GONE);
			tvRegister.setText("确定");
		}
		tvTip.setText(Html.fromHtml(
				"<font  color=#383838>点击注册，即表示您同意</font><font  color=#28d46d>《粒到农资注册协议》</font>"));
		tvTip.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(RegisterActivity.this,
						RegisterAgreementActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_register:
			String phone = etPhone.getText().toString().trim();
			String sms = etSms.getText().toString().trim();
			String psd = etPsd.getText().toString().trim();
			String psdAgain = etPsdAgain.getText().toString().trim();
			if (TextUtils.isEmpty(psd)) {
				showToast("请输入密码");
				return;
			}
			if (TextUtils.isEmpty(psdAgain)) {
				showToast("请再次输入密码");
				return;
			}

			showDialog("");
			if (isForgetPSWD) {
				BusinessManager.getInstance().sendForgetVerify(
						new ForgetPswRequest(phone, psd, psdAgain, sms),
						new MyCallback<Void>() {
							@Override
							public void onSuccess(Void result, String message) {
								showToast("密码修改成功，请登陆！");
								dismissDialog();
								finish();
							}

							@Override
							public void onError(String errorCode,
									String message) {
								showToast(message);
								dismissDialog();
							}

							@Override
							public void onFinished() {

							}

							@Override
							public void onRefreshToken() {

							}
						});

			} else {
				if (!psd.equals(psdAgain)) {
					showToast("您两次输入的密码不一致");
					dismissDialog();
					return;
				}
				BusinessManager.getInstance().sendRegisterVerify(
						new RegisterRequest(phone, psd, psdAgain, sms),
						new MyCallback<Void>() {
							@Override
							public void onSuccess(Void result, String message) {
								showToast("注册成功，请登陆");
								dismissDialog();
								finish();
							}

							@Override
							public void onError(String errorCode,
									String message) {
								showToast(message);
								dismissDialog();
							}

							@Override
							public void onFinished() {

							}

							@Override
							public void onRefreshToken() {

							}
						});
			}
			break;
		case R.id.tv_send_sms:
			String phone1 = etPhone.getText().toString().trim();
			if (TextUtils.isEmpty(phone1)) {
				showToast("请输入手机号码");
				return;
			}
			showDialog("");
			if (isForgetPSWD) {
				BusinessManager.getInstance().sendForgetSms(
						new ForgetPswSmsRequest(phone1),
						new MyCallback<Void>() {
							@Override
							public void onSuccess(Void result, String message) {
								showToast("发送成功");
								countDown();
								dismissDialog();
							}

							@Override
							public void onError(String errorCode,
									String message) {
								showToast(message);
								dismissDialog();
							}

							@Override
							public void onFinished() {

							}

							@Override
							public void onRefreshToken() {

							}
						});
			} else {
				BusinessManager.getInstance().sendRegisterSms(
						new RegisterSmsRequest(phone1),
						new MyCallback<Void>() {
							@Override
							public void onSuccess(Void result, String message) {
								// showToast("注册成功，请登陆");
								// dismissDialog();
								// finish();
								showToast("发送成功");
								dismissDialog();
							}

							@Override
							public void onError(String errorCode,
									String message) {
								showToast(message);
								dismissDialog();
							}

							@Override
							public void onFinished() {

							}

							@Override
							public void onRefreshToken() {

							}
						});
			}
			break;
		}
	}

	private Timer mTimer ;

	private void countDown() {
		mTimer = new Timer();
		tvSendSms.setClickable(false);
		TimerTask task = new TimerTask() {
			int count = 60;

			@Override
			public void run() {

				runOnUiThread(new Runnable() { // UI thread
					@Override
					public void run() {
						tvSendSms.setTextColor(
								getResources().getColor(R.color.common_text_6));
						count--;
						tvSendSms.setText(count + "秒后重新获取");
						if (count == 0) {
							mTimer.cancel();
							tvSendSms.setClickable(true);
							tvSendSms.setText("获取验证码");
							tvSendSms.setTextColor(getResources().getColor(R.color.common_green));
						}
					}
				});
			}
		};
		mTimer.schedule(task, 0, 1000);
	}

	@Override
	protected void onDestroy() {
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		super.onDestroy();
	}
}
